package com.huage.courseplatform.application.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huage.courseplatform.application.model.dto.CourseDTO;
import com.huage.courseplatform.application.model.dto.PeriodDTO;
import com.huage.courseplatform.application.model.dto.PeriodTemplateDTO;
import com.huage.courseplatform.application.model.dto.PeriodTemplateItemDTO;
import com.huage.courseplatform.application.model.entity.Course;
import com.huage.courseplatform.application.model.entity.PeriodTemplate;
import com.huage.courseplatform.application.model.entity.PeriodTemplateItem;
import com.huage.courseplatform.application.model.param.CoursePARAM;
import com.huage.courseplatform.application.model.param.PeriodGeneratePARAM;
import com.huage.courseplatform.application.model.param.PeriodTemplateItemPARAM;
import com.huage.courseplatform.application.model.param.PeriodTemplatePARAM;
import com.huage.courseplatform.application.model.vo.CoursePeriodVO;
import com.huage.courseplatform.application.model.vo.PeriodTemplateAndItemVO;
import com.huage.courseplatform.application.service.PeriodTemplateItemService;
import com.huage.courseplatform.application.service.PeriodTemplateService;
import com.huage.courseplatform.framework.annotation.RepeatSubmit;
import com.huage.courseplatform.framework.api.AbstractController;
import com.huage.courseplatform.framework.api.BaseController;
import com.huage.courseplatform.framework.model.param.PagePARAM;
import com.huage.courseplatform.framework.utils.BeanCopyUtils;
import com.huage.courseplatform.framework.utils.ResultUtils;
import com.huage.courseplatform.framework.utils.date.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.huage.courseplatform.framework.utils.date.DateUtils.YYYY_MM_DD;


@RestController
@RequestMapping(value = "/periodTemplate")
@Api(tags = "课时模板接口")
@Slf4j
@SaCheckRole({"ROLE_ADMIN","ROLE_BUSINESS"})
public class PeriodTemplateController extends AbstractController<PeriodTemplate, PeriodTemplateDTO, PeriodTemplatePARAM> {

    @Resource
    PeriodTemplateService periodTemplateService;

    @RepeatSubmit
    @ApiOperation(value = "添加（模板和明细）")
    @PostMapping(value = "/add")
    public ResultUtils<PeriodTemplateDTO> add(@RequestBody @Validated({Insert.class}) PeriodTemplatePARAM periodTemplatePARAM) {
        List<PeriodTemplateItem> periodTemplateItemList = BeanCopyUtils.copyWithCollection(periodTemplatePARAM.getPeriodTemplateItemPARAMList(),PeriodTemplateItem.class);
        PeriodTemplate periodTemplate =  paramToEntity(periodTemplatePARAM);
        periodTemplateService.addPeriodTemplateAndItem(paramToEntity(periodTemplatePARAM),periodTemplateItemList);
        return ResultUtils.success(entityToDto(periodTemplate));
    }

    @RepeatSubmit
    @ApiOperation(value = "更新（模板和明细）")
    @PutMapping(value = "/updateTemplateAndItem")
    public ResultUtils<PeriodTemplateDTO> updateTemplateAndItem(@RequestBody @Validated({Update.class}) PeriodTemplatePARAM periodTemplatePARAM) {
        List<PeriodTemplateItem> periodTemplateItemList = BeanCopyUtils.copyWithCollection(periodTemplatePARAM.getPeriodTemplateItemPARAMList(),PeriodTemplateItem.class);
        PeriodTemplate periodTemplate =  paramToEntity(periodTemplatePARAM);
        periodTemplateService.updatePeriodTemplateAndItem(periodTemplate,periodTemplateItemList);
        return ResultUtils.success(entityToDto(periodTemplate));
    }

    @RepeatSubmit
    @ApiOperation(value = "更新（无明细）")
    @PutMapping(value = "/update")
    public ResultUtils<PeriodTemplateDTO> update(@RequestBody @Validated({Update.class}) PeriodTemplatePARAM periodTemplatePARAM) {
        PeriodTemplate periodTemplate =  paramToEntity(periodTemplatePARAM);
        periodTemplateService.saveOrUpdate(periodTemplate);
        return ResultUtils.success(entityToDto(periodTemplate));
    }


    @ApiOperation(value = "分页查询(无明细)")
    @PostMapping(value = "/page")
    public ResultUtils<Page<PeriodTemplateDTO>> getByPage(@Validated @RequestBody PagePARAM pagePARAM){
        Page<PeriodTemplate> page = new Page(pagePARAM.getPageNum(),pagePARAM.getPageSize());
        QueryWrapper<PeriodTemplate> qw =  super.getQueryWrapperByPageParam(pagePARAM);
        return ResultUtils.success(entityToDto(periodTemplateService.page(page,qw)));
    }

    @SaIgnore
    @ApiOperation(value = "根据id查询模板和明细")
    @GetMapping(value = "/getCourseAndPeriodById/{id}")
    public ResultUtils<List<PeriodTemplateAndItemVO>> getCourseAndPeriodById(@PathVariable String id) {
        return ResultUtils.success(periodTemplateService.getPeriodTemplateAndItem(id));
    }

    @ApiOperation(value = "根据id删除")
    @DeleteMapping(value = "/delete/{id}")
    public ResultUtils<String> deleteById(@PathVariable String id) {
        periodTemplateService.delPeriodTemplateAndItemById(id);
        return ResultUtils.success(id);
    }

    @Deprecated
    @ApiOperation(value = "根据id集合删除(不使用)")
    @DeleteMapping(value = "/delete")
    public ResultUtils<List<String>> deleteByIds(@RequestBody List<String> ids) {
        periodTemplateService.delPeriodTemplateAndItemByIds(ids);
        return ResultUtils.success(ids);
    }

    @ApiOperation(value = "根据模板生成课时")
    @PostMapping(value = "/generatePeriod")
    public ResultUtils<?> generatePeriod(@RequestBody PeriodGeneratePARAM periodGeneratePARAM){
        Date startDay = DateUtils.dateTime(YYYY_MM_DD,periodGeneratePARAM.getStartDay());
        Date endDay = DateUtils.dateTime(YYYY_MM_DD,periodGeneratePARAM.getEndDay());
        Assert.isTrue(startDay.before(endDay),"结束时间不能在开始时间之前");
        periodTemplateService.generatePeriod(periodGeneratePARAM.getTemplateId(),periodGeneratePARAM.getBusinessId(),startDay,endDay);
        return ResultUtils.success("生成成功",null);
    }
}
